﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.App;
using Domain.Types;
using FacadeApi.Extension;
using Infrastructure.Error;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace FacadeApi.Controllers
{


    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class SmsController : ControllerBase
    {
        public UserApplication userApplication { get; set; }
        public ILogger<SmsController> _logger { get; set; }

        /// <summary>
        /// 发送短信  DeviceToken
        /// </summary>
        /// <param name="Phone">FromForm</param>
        /// <param name="smsCodeType">FromForm 0：登录注册</param>
        /// <returns></returns>
        [HttpPost]
        [TokenValidFilter]
        public ActionResult SendSms([FromForm]string Phone, [FromForm] SmsCodeType smsCodeType)
        {
            string ipaddress = this.HttpContext.Connection.RemoteIpAddress.ToString();

            try
            {
                userApplication.SendSms(Phone, smsCodeType, ipaddress);
                return this.Success(null,"短信发送成功!");
            }
            catch (BizException ex)
            {
                return this.FailResult(ex.data, ex.Message);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                return this.FailResult(null, "系统繁忙，请稍后重试!");
            }
        }



    }
}
